package toledo.rest;

import java.util.List;
import java.util.Vector;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import toledo.util.DocumentList;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;

@Path("/course")
public class Course
{
	private static final String USERNAME = "brian.pieter.toledo";
	private static final String PASSWORD = "Toledo2011";
	
	public static final String DATABASE = "courses";
	public static final String COURSE_ID = "course_id";
	public static final String COURSENAME = "coursename";
	public static final String DOCS_RES_ID = "doc_id";
	public static final String DOCS_PUBLIC_URL = "docs_public_url";
	public static final String HASHTAG = "hashtag";
	public static final String KEY = Entity.KEY_RESERVED_PROPERTY;
	
	public static Entity getCourse(Object course_id)
	{
		try
		{
			return DatastoreServiceFactory.getDatastoreService().get(KeyFactory.stringToKey(course_id.toString()));
		}
		catch(EntityNotFoundException e)
		{
		}
		return null;
	}
	
	@Path("/entries")
	@GET
	@Produces("application/json")
	public String getEntries(@QueryParam(Course.COURSE_ID)String course_id)
	{
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		JSONObject json = new JSONObject();
		Vector<JSONObject> list = new Vector<JSONObject>();
		
		List<Entity> entries = ds.prepare(new Query(Entry.DATABASE).addSort(Entity.KEY_RESERVED_PROPERTY,SortDirection.DESCENDING).addFilter(Entry.COURSE_ID,FilterOperator.EQUAL,course_id).addFilter(Entry.REPLY_TO_ID,FilterOperator.EQUAL,"")).asList(FetchOptions.Builder.withDefaults());
		for(Entity entry:entries)
		{
			try
			{
				JSONObject o = new JSONObject();
				Entity user = User.getUser(entry.getProperty(Entry.USER_ID));
				String key = KeyFactory.keyToString(entry.getKey());
				o.put(Entry.ENTRY_ID,key);
				o.put(User.FULLNAME,user.getProperty(User.FULLNAME));
				o.put(User.PIC_URL,user.getProperty(User.PIC_URL));
				o.put(Entry.DATE,entry.getProperty(Entry.DATE));
				o.put(Entry.TYPE,entry.getProperty(Entry.TYPE));
				o.put(Entry.TEXT,entry.getProperty(Entry.TEXT));
				o.put(Entry.NUM_REPLIES,Entry.getComments(key).size());
				list.add(o);
			}
			catch(JSONException e)
			{
			}
		}
		
		JSONArray results = new JSONArray(list);
		try
		{
			Entity course = Course.getCourse(course_id);
			//json.put("total_results", results.length());
			json.put(Course.COURSENAME,course.getProperty(Course.COURSENAME));
			json.put(Course.HASHTAG,course.getProperty(Course.HASHTAG));
			json.put("results", results);
		}
		catch (JSONException e)
		{
		}
		
		return json.toString();
	}
	
	@Path("/documents")
	@GET
	@Produces("application/json")
	public String getDocuments(@QueryParam(Course.COURSE_ID)String course_id)
	{
		Entity course = Course.getCourse(course_id);
		JSONObject json = new JSONObject();
		Vector<JSONObject> list = new Vector<JSONObject>();
		
		try
		{
			DocumentList dl = new DocumentList("Toledo2011");
			dl.login(USERNAME,PASSWORD);
			DocumentListFeed dlf = dl.getFolderDocsListFeed(course.getProperty(Course.DOCS_RES_ID).toString());
			for(DocumentListEntry dle:dlf.getEntries())
			{
				JSONObject o = new JSONObject();
				o.put("filename",dle.getTitle().getPlainText());
				o.put("type",dle.getType());
				o.put("last_modified",dle.getUpdated().toString());
				o.put("href",dle.getDocumentLink().getHref());
				//o.put("description",dle.getDescription());
				list.add(o);
			}
		}
		catch(Exception e)
		{
		}
		
		JSONArray results = new JSONArray(list);
		try
		{
			json.put(Course.COURSENAME,course.getProperty(Course.COURSENAME));
			json.put("results",results);
		}
		catch (JSONException e)
		{
		}
		
		return json.toString();
	}
}
